package com.wistech.solr.ik;

import java.util.Collection;

import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.cfg.DefaultConfig;
import org.wltea.analyzer.dic.Dictionary;

import com.google.common.collect.Sets;

public abstract class IKWordProvider {

	public IKWordProvider() {
		Configuration cfg = DefaultConfig.getInstance();
		cfg.setUseSmart(true);
		Dictionary.initial(cfg);
	}

	public void addWord(String word) {
		addWordToIKDictionary(word);
		publishWord(word);
		saveWord(word);
	}

	public abstract void loadWords();

	protected abstract void saveWord(String word);

	public abstract void removeWord(String word);

	public abstract void cleanWords();

	protected abstract void publishWord(String word);

	protected void addWordToIKDictionary(String... words) {
		if (words != null && words.length > 0) {
			Collection<String> collection = Sets.newHashSet();
			for (String word : words) {
				collection.add(word);
			}
			Dictionary.getSingleton().addWords(collection);
		}
	}

	protected void addWordToIKDictionary(Collection<String> words) {
		if (words != null && !words.isEmpty())
			Dictionary.getSingleton().addWords(words);
	}
}
